package hn.cch.advanced.concurrent;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class SemaphoreDemo {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(7);
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 10; i++) {
            executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + ":begin");
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName() + ":end");
                    semaphore.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
        executorService.shutdown();
        ExecutorServiceUtil.complete(executorService);

    }

}
